package com.avit.tv.kui.pro.volume;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import com.avit.tv.kui.utils.Log;
import com.avit.tv.kui.utils.Utils;

/**
 * 音量条
 *
 * @author like
 * @date 2020/8/28
 */
public class VolumeProgressView extends View {

    @ColorInt
    private int normalColor = Color.DKGRAY;
    @ColorInt
    private int progressColor = Color.WHITE;

    /**
     * 自定义默认底部进度
     */
    private Path normalPath;
    private Paint normalPaint;

    /**
     * 自定义进度显示
     */
    private Path progressPath;
    private Paint progressPaint;

    private float barHeight;

    private RectF showRect;

    private float currentX;
    private float baseLineY;
    private float moveX;


    private float startX;
    private float maxX;
    private int maxHeight;

    public VolumeProgressView(Context context) {
        this(context, null);
    }

    public VolumeProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VolumeProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        barHeight = 1.5f* Utils.getRadix(context);
        showRect = new RectF();
        initNormalPaint();
        initProgressPaint();
    }

    private void initNormalPaint() {
        normalPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        normalPath = new Path();
        normalPaint.setColor(normalColor);
        normalPaint.setStyle(Paint.Style.STROKE);
        normalPaint.setPathEffect(new CornerPathEffect(barHeight));
        normalPaint.setStrokeWidth(barHeight);
    }

    private void initProgressPaint() {
        progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        progressPath = new Path();
        progressPaint.setColor(progressColor);
        progressPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        progressPaint.setPathEffect(new CornerPathEffect(barHeight));
        progressPaint.setStrokeWidth(barHeight);
    }

    /**
     * 绘制默认的进度
     *
     * @param canvas
     */
    private void drawDefault(Canvas canvas) {
        normalPath.reset();
        normalPath.moveTo(0, baseLineY);
        normalPath.lineTo(getMeasuredWidth(), baseLineY);
        canvas.drawPath(normalPath, normalPaint);
        canvas.save();
    }

    /**
     * 绘制进度
     *
     * @param canvas
     */
    private void drawProgress(Canvas canvas) {
        maxX = getMeasuredWidth() -  barHeight-1;
        startX = currentX + 2 * barHeight;
        progressPath.reset();
        progressPath.moveTo(currentX, baseLineY);
        progressPath.lineTo(0, baseLineY);
        progressPath.addCircle(Math.min(startX, maxX), baseLineY, barHeight, Path.Direction.CW);
        canvas.drawPath(progressPath, progressPaint);
    }

    /**
     * volume down
     */
    public void down() {
        Log.w(this, "current:" + currentX + "====Max:" + maxX);
        if (currentX-moveX > 0) {
            currentX -= moveX;
        } else
            currentX = 0;
        invalidate();
    }

    /**
     * volume up
     */
    public void up() {
        if (currentX < maxX) {
            currentX += moveX;
        } else
            currentX = maxX;
        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawDefault(canvas);
        drawProgress(canvas);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (heightMode == MeasureSpec.EXACTLY) {
            heightSize = heightSize <= maxHeight ? heightSize
                    : (int) maxHeight;
        }

        if (heightMode == MeasureSpec.UNSPECIFIED) {
            heightSize = heightSize <= maxHeight ? heightSize
                    : (int) maxHeight;
        }
        if (heightMode == MeasureSpec.AT_MOST) {
            heightSize = heightSize <= maxHeight ? heightSize
                    : (int) maxHeight;
        }
        int maxHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize,
                heightMode);
        super.onMeasure(widthMeasureSpec, maxHeightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        showRect.left = left;
        showRect.right = right;
        showRect.top = top;
        showRect.bottom = bottom;
        baseLineY = Math.abs(showRect.bottom - showRect.top) / 2;
    }

    /**
     * 设置音量条默认颜色
     *
     * @param normalColor
     */
    public void setNormalColor(@ColorInt int normalColor) {
        this.normalColor = normalColor;
    }

    /**
     * 设置音量条颜色
     *
     * @param progressColor
     */
    public void setProgressColor(@ColorInt int progressColor) {
        this.progressColor = progressColor;
        invalidate();
    }

    /**
     * 设置当前的位置
     *
     * @param currentX
     */
    public void setCurrentX(float currentX) {
        this.currentX = currentX;
        invalidate();
    }

    /**
     * 设置单位长度的move
     *
     * @param
     */
    public void setMoveX(float moveX) {
        this.moveX = moveX;
    }

    /**
     * 设置最大高度
     *
     * @param maxHeight
     */
    public void setMaxHeight(int maxHeight) {
        this.maxHeight = maxHeight;
    }

    /**
     * 获取偏移量
     * @return
     */
    public float getMoveX() {
        return moveX;
    }
}
